<template>
  <div class="addScheduling_container">
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="580px"
      :before-close="handleClose"
    >
      <div class="">
        <el-form
          :model="formSemester"
          :rules="rules"
          ref="semesterForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="类型" prop="type">
            <el-radio-group class="radio_group" v-model="formSemester.type">
              <el-radio :label="0">必修</el-radio>
              <el-radio :label="1">选修</el-radio>
              <el-radio :label="2">考证</el-radio>
              <el-radio :label="3">工学</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="学分" prop="credit">
            <el-input
          v-input-restrict-number
              class="semesterName"
              :step="0.1"
              :precision="1"
              v-model="formSemester.credit"
              controls-position="right"
              placeholder="请输入学分"
              :min="0"
            />
            分
          </el-form-item>
          <el-form-item label="选择课程" prop="courseId">
            <el-select
              filterable
              v-model="formSemester.courseId"
              class="semesterName"
              clearable
              placeholder="请选择课程"
            >
              <el-option
                :disabled="selectedCourseIds.indexOf(item.courseId) > -1"
                v-for="item in courseList"
                :key="item.courseId"
                :label="item.courseName"
                :value="item.courseId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <div style="display: flex">
            <el-form-item label="学习时间" prop="beginDate">
              <el-date-picker
                class="date_picker"
                :picker-options="pickerOptionsEnd"
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="formSemester.beginDate"
                type="date"
                clearable
                placeholder="开始日期"
              >
              </el-date-picker>
            </el-form-item>
            <span style="margin: 7px 10px 0">至</span>
            <el-form-item prop="endDate" label-width="0px">
              <el-date-picker
                class="date_picker"
                :picker-options="pickerOptionsStart"
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="formSemester.endDate"
                type="date"
                clearable
                placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </div>
        </el-form>
        <div class="dialog_footer">
          <el-button v-preventReClick @click="handleClose">取 消</el-button>
          <el-button
            v-preventReClick
            type="primary"
            @click="submitForm('semesterForm')"
            >确 定
          </el-button>
        </div>
      </div>
    </el-dialog>
    <div class="empty_box" v-if="branchList.length == 0">
      <el-empty description="暂无数据"></el-empty>
    </div>
    <div
      v-if="branchList.length > 0"
      style="
        display: flex;
        border-radius: 20px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      "
    >
      <div class="branch_name_all">
        <div
          class="branch_name"
          @click="swithActive(item, index)"
          :class="{ active_name: item.branch_id == activeId }"
          v-for="(item, index) in branchList"
          :key="item.branch_id"
        >
          {{ item.branch_name }}
        </div>
      </div>
      <div class="el_collapse" v-if="majorList.length > 0">
        <el-collapse
          accordion
          slot="title"
          style="width: 100%"
          @change="handleChange"
          v-model="activeNames"
        >
          <el-collapse-item
            class="mb_10"
            :name="childItem.major_id"
            v-for="childItem in majorList"
            :key="childItem.major_id"
          >
            <template slot="title">
              <i
                :class="{ collapsed: activeNames == childItem.major_id }"
                class="el-icon-arrow-right"
              ></i>
              <span
                class="title_list"
                :class="{ color_green: activeNames == childItem.major_id }"
                >{{ childItem.major_name }}</span
              >
            </template>
            <div v-for="sonItem in childItem.classList" :key="sonItem.class_id">
              <div class="class_name">
                {{ sonItem.class_name }}
                <el-button
                  v-preventReClick
                  @click="showDialog(1, childItem.major_id, sonItem.class_id)"
                  type="primary"
                  style="margin-left: 10px"
                  size="mini"
                  icon="el-icon-plus"
                  >添加
                </el-button>
              </div>
              <div
                class="flex_center"
                v-if="sonItem.courseSchedulingList.length > 0"
              >
                <div
                  class="content_item"
                  v-for="courseSchedulingItem in sonItem.courseSchedulingList"
                  :key="courseSchedulingItem.id"
                >
                  <img
                    class="book_img"
                    src="../../assets/images/book_img.png"
                    alt=""
                  />
                  <div class="content_item_coursename">
                    {{
                      courseSchedulingItem.courseName
                        ? courseSchedulingItem.courseName
                        : "暂无"
                    }}
                  </div>
                  <div class="content_item_title">
                    <span class="public_font">
                      ({{ typeText[courseSchedulingItem.type] }})
                    </span>
                  </div>
                  <div class="content_item_title">
                    <span class="public_font">
                      {{
                        courseSchedulingItem.teacherName
                          ? courseSchedulingItem.teacherName
                          : "暂无"
                      }}
                    </span>
                  </div>
                  <div class="content_item_title">
                    <span class="public_font">
                      {{ courseSchedulingItem.credit }}学分
                    </span>
                  </div>
                  <div class="content_item_time">
                    {{ courseSchedulingItem.beginDate | fammterTime }}~{{
                      courseSchedulingItem.endDate | fammterTime
                    }}
                  </div>
                  <div class="absolute">
                    <i
                      @click="showDialog(2, courseSchedulingItem)"
                      class="el-icon-edit"
                    ></i>
                    <i
                      @click="delCourse(courseSchedulingItem.id)"
                      class="el-icon-error"
                    ></i>
                  </div>
                </div>
              </div>
              <div
                class="courseSchedulingList_box"
                v-if="sonItem.courseSchedulingList.length == 0"
              >
                <el-empty description="暂无数据"></el-empty>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
      <div class="empty_box" v-if="majorList.length == 0">
        <el-empty description="暂无数据"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getCourseSchedulingList,
  getCourseList,
  addCourseScheduling,
  updateCourseScheduling,
  deleteCourseScheduling,
} from "@/api/schoolSemester/schoolSemester";

export default {
  name: "addScheduling",
  data() {
    return {
      activeId: "",
      activeIndex: 0,
      loaading: false,
      title: "添加课程",
      pickerOptionsStart: {
        // 时间只能大于startTime
        disabledDate: (time) => {
          if (!this.formSemester.beginDate) {
            return;
          }
          return (
            time.getTime() < new Date(this.formSemester.beginDate).getTime()
          );
        },
      },
      pickerOptionsEnd: {
        // 时间只能小于startTime
        disabledDate: (time) => {
          if (!this.formSemester.endDate) {
            return;
          }
          return time.getTime() > new Date(this.formSemester.endDate).getTime();
        },
      },
      dialogVisible: false,
      formSemester: {
        type: 0,
        credit: undefined,
        courseId: "",
        beginDate: "",
        endDate: "",
      },
      rules: {
        courseId: [
          { required: true, message: "请输入学期名称", trigger: "change" },
        ],
        type: [{ required: true, message: "", trigger: "blur" }],
        credit: [{ required: true, message: "学分不能为空" }],
        endDate: [
          { required: true, message: "结束日期不能为空", trigger: "change" },
        ],
        beginDate: [
          { required: true, message: "开始日期不能为空", trigger: "change" },
        ],
      },
      branchList: [],
      courseList: [],
      majorList: [],
      activeNames: "",
      selectedCourseIds: "",
      typeText: {
        0: "必修",
        1: "选修",
        2: "考证",
        3: "工学",
      },
    };
  },
  filters: {
    fammterTime(val) {
      if (!val) {
        return "";
      }
      var d = new Date(val);
      var year = d.getFullYear();
      var month = d.getMonth() + 1;
      var date = d.getDate();
      var nowTime = `${year}-${month < 10 ? "0" + month : month}-${
        date < 10 ? "0" + date : date
      }`;

      return nowTime;
    },
  },
  created() {
    this.getCourseSchedulingList();
  },
  methods: {
    swithActive(item, index) {
      this.activeId = item.branch_id;
      this.activeIndex = index;
      this.majorList = this.branchList[index].majorList;
    },
    async showDialog(type, item, classId) {
      this.title = type == 1 ? "添加课程" : "编辑课程";
      this.formSemester.beginDate = this.$route.query.beginTime;
      this.formSemester.endDate = this.$route.query.endTime;
      if (type == 2) {
        this.formSemester.credit = item.credit;
        this.formSemester.id = item.id;
        this.formSemester.courseId = item.courseId;
        this.formSemester.classId = item.classId;
        this.formSemester.majorId = item.majorId;
        this.formSemester.semesterId = item.semesterId;
        this.formSemester.beginDate = item.beginDate;
        this.formSemester.endDate = item.endDate;
      } else {
        this.formSemester.classId = classId;
        this.formSemester.majorId = item;
        this.formSemester.semesterId = this.$route.query.id;
      }
      let res = await getCourseList(
        this.$route.query.id,
        type == 1 ? classId : item.classId
      );
      const { code, data } = res;
      if (code == 200) {
        this.courseList = data.courseList;
        this.selectedCourseIds = data.selectedCourseIds;
        this.dialogVisible = true;
      }
    },
    handleClose() {
      (this.formSemester = {
        type: 0,
        credit: "",
        courseId: "",
        beginDate: "",
        endDate: "",
      }),
        this.$refs["semesterForm"].resetFields();
      this.dialogVisible = false;
    },
    async getCourseSchedulingList(type) {
      let res = await getCourseSchedulingList(this.$route.query.id);
      const { code, data } = res;
      if (code == 200) {
        this.branchList = data.branchList;
        if (data.branchList && data.branchList.length > 0) {
          if (!type) {
            this.activeId = data.branchList[0].branch_id;
            this.majorList = data.branchList[0].majorList;
          } else {
            this.activeId = data.branchList[this.activeIndex].branch_id;
            this.majorList = data.branchList[this.activeIndex].majorList;
          }
        }
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = {
            ...this.formSemester,
            beginDate:this.formSemester.beginDate.slice(0,10) + ' ' + '00:00:00',
            endDate:this.formSemester.endDate.slice(0,10) + ' ' + '23:59:59'
          };
          if (this.title == "添加课程") {
            console.log(params, "params");
            let res = await addCourseScheduling(params);
            const { code } = res;
            if (code == 200) {
              this.$message.success("添加成功");
              this.$refs["semesterForm"].resetFields();
              this.dialogVisible = false;
              this.getCourseSchedulingList("refresh");
            }
          } else {
            console.log(params, "params");
            let res = await updateCourseScheduling(params);
            const { code } = res;
            if (code == 200) {
              this.$message.success("修改成功");
              this.$refs["semesterForm"].resetFields();
              this.dialogVisible = false;
              this.getCourseSchedulingList("refresh");
            }
          }
        } else {
          return false;
        }
      });
    },
    delCourse(id) {
      this.$confirm("确认要删除该排课吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteCourseScheduling(id);
          const { code } = res;
          if (code == 200) {
            this.$message.success("删除成功！");
            this.getCourseSchedulingList("refresh");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleChange(val) {
      console.log(val, "valvalval");
      this.activeNames = val;
    },
  },
};
</script>

<style scoped lang="scss">
.branch_name_all {
  padding: 30px 0;
  box-sizing: border-box;
  width: 208px;
  border-right: 1px solid rgba(229, 229, 229, 1);
}
.branch_name {
  width: 100%;
  height: 40px;
  font-size: 16px;
  font-weight: bold;
  color: rgba(128, 128, 128, 1);
  cursor: pointer;
  user-select: none;
  display: flex;
  justify-content: center;
  align-items: center;
}
.branch_name:hover {
  background: #28b28b;
  color: #ffffff !important;
}
.active_name {
  background: #28b28b;
  color: #ffffff !important;
}
.addScheduling_container {
  padding: 20px;
  box-sizing: border-box;
}
.el_collapse {
  padding: 30px 20px;
  box-sizing: border-box;
  width: 100%;
  height: 680px;
  overflow: auto;
}
.el-icon-arrow-right,
.el-icon-arrow-down {
  margin-right: 4px;
  font-size: 20px;
  font-weight: 400;
  color: rgba(128, 128, 128, 1);
}
.title_list {
  font-size: 16px;
  font-weight: 600;
  color: rgba(128, 128, 128, 1);
}
.class_name {
  width: 100%;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebeef5;
  padding: 10px 0 10px 50px;
  box-sizing: border-box;
}

.mb_10 {
  margin-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}
.collapse_container {
  width: 100%;
}
.flex_center {
  width: 100%;
  display: flex;
  padding: 10px 0 10px 50px;
  box-sizing: border-box;
  flex-wrap: wrap;
}
.content_item {
  width: 222px;
  height: 132px;
  border-radius: 8px;
  background: linear-gradient(
    135deg,
    rgba(38, 191, 189, 1) 0%,
    rgba(0, 225, 181, 1) 100%
  );
  box-shadow: 0 1px 4px rgba(40, 178, 139, 0.4);
  display: flex;
  padding: 9px 20px;
  box-sizing: border-box;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  margin: 0 0 10px 16px;
  &_coursename {
    font-size: 16px;
    font-weight: 600;
    color: white;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  &_title {
    font-size: 12px;
    font-weight: 400;
    color: white;
  }
  &_time {
    font-size: 12px;
    font-weight: 400;
    color: white;
  }
  &:hover {
    .absolute {
      display: flex;
    }
  }
}
.book_img {
  width: 110px;
  height: 120px;
  position: absolute;
  top: 10px;
  right: -30px;
}
.public_font {
  font-size: 12px;
  font-weight: 400;
  color: white;
}
.el-icon-error {
  margin-left: 6px;
  font-size: 16px;
  color: white;
  cursor: pointer;
}
.el-icon-edit {
  font-size: 16px;
  color: #ffffff;
  cursor: pointer;
}
.absolute {
  display: none;
  position: absolute;
  top: 6px;
  right: 6px;
  align-items: center;
}
.semesterName {
  width: 314px;
}
.radio_group {
  width: 300px;
}
.dialog_footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.color_green {
  color: #28b28b;
}
.collapsed {
  transform: rotate(90deg);
  color: #28b28b;
}
.el-icon-arrow-right {
  transition: all 0.2s ease;
}
.empty_box {
  width: 100%;
  padding: 10vh 0;
  box-sizing: border-box;
  height: 680px;
}
.courseSchedulingList_box {
  width: 100%;
  height: 280px;
}
.date_picker {
  width: 140px;
}
</style>
